# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import traceback
import json

from django.contrib.messages.storage import session
from django.http import JsonResponse
from utils.logger import Md_Debug
from app.project.common.project_query import ProjectDB, ProjectQueryDB
from app.project.common.project_add_edit_delete import ProjectEditDB
from django.views.decorators.csrf import csrf_exempt
from utils.response_utils import response_change
from utils.backcode import ImportError

project_db = ProjectDB()
ProjectQueryDB = ProjectQueryDB()
ProjectEditDB = ProjectEditDB()


def public_view(request, func, session_content=False):
    data = dict()
    try:
        body = json.loads(request.body)
        if session_content == True:
            body["session_user_id"] = request.session.get("session_user_id")
            body["session_user_name"] = request.session.get("name")
            body["session_company_post"] = request.session.get("company_post")
            body["session_department"] = request.session.get("department")
            if body["session_user_id"] == None:
                data = {"response_msg": "params is error", "response_code": ImportError.IMPORT_CODE_EXECUTE_FAILED}
                raise ("session is error")
        data = func(body)
    except Exception as e:
        Md_Debug.info(traceback.format_exc(e))
        print(traceback.format_exc(e))
    response = JsonResponse(data)
    return response_change(response)


@csrf_exempt
def add_project(request):
    """添加项目"""
    return public_view(request, ProjectEditDB.add_project)


@csrf_exempt
def del_project(request):
    """删除项目"""
    return public_view(request, ProjectEditDB.del_project)


@csrf_exempt
def edit_project(request):
    """编辑项目"""
    return public_view(request, ProjectEditDB.edit_project)


@csrf_exempt
def project_list(request):
    """项目列表"""
    return public_view(request, ProjectQueryDB.project_list)


@csrf_exempt
def project_info(request):
    """项目信息"""
    return public_view(request, ProjectQueryDB.project_info)


@csrf_exempt
def project_over(request):
    """项目信息"""
    return public_view(request, ProjectEditDB.project_over)


@csrf_exempt
def add_project_group(request):
    """添加项目团队"""
    return public_view(request, ProjectEditDB.add_project_member)


@csrf_exempt
def del_project_group(request):
    """删除项目团队"""
    return public_view(request, ProjectEditDB.del_project_member)


@csrf_exempt
def edit_project_group(request):
    """编辑项目团队"""
    return public_view(request, ProjectEditDB.edit_project_member)


@csrf_exempt
def project_group_list(request):
    """项目团队列表"""
    return public_view(request, ProjectQueryDB.project_member_list)


@csrf_exempt
def project_group_info(request):
    """项目团队信息"""
    
    return public_view(request, ProjectQueryDB.project_member_info)


@csrf_exempt
def project_group_edit_user(request):
    """编辑项目团队成员"""
    return public_view(request, ProjectEditDB.project_member_edit_user)


@csrf_exempt
def project_group_del_user(request):
    """删除项目团队成员"""
    return public_view(request, ProjectEditDB.project_member_del_user)


@csrf_exempt
def project_leader_list(request):
    # 项目领导人列表
    return public_view(request, ProjectQueryDB.project_leader_list, True)


# @csrf_exempt
# def project_info_person(request):
#     return public_view(request, ProjectQueryDB.project_info_person,True)

@csrf_exempt
def project_info_person(request):
    company_post = request.session.get("company_post")
    if company_post >= 3:
        return public_view(request, ProjectQueryDB.project_info_person_all, True)
    return public_view(request, ProjectQueryDB.project_info_person, True)


@csrf_exempt
def project_empty_person(request):
    return public_view(request, ProjectQueryDB.project_empty_person)


@csrf_exempt
def project_edit_leader(request):
    return public_view(request, ProjectEditDB.project_edit_leader)


@csrf_exempt
def project_group_list_info(request):
    return public_view(request, ProjectQueryDB.project_group_list_info, True)


@csrf_exempt
def project_search(request):
    return public_view(request, ProjectQueryDB.project_search,True)
